package com.example.droidcafe;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class OrderActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order);

    String orderMsg = getIntent().getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView orderText = findViewById(R.id.order_text);
    orderText.setText(orderMsg);
  }

  @SuppressLint("NonConstantResourceId")
  public void onRadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();
    switch (view.getId()) {
      case R.id.sameday:
        if (checked) {
          displayToast(getString(R.string.same_day_messenger_service));
        }
        break;
      case R.id.nextday:
        if (checked)
          // Next day delivery
          displayToast(getString(R.string.next_day_ground_delivery));
        break;
      case R.id.pickup:
        if (checked)
          // Pick up
          displayToast(getString(R.string.pick_up));
        break;
      default:
        // Do nothing.
        break;
    }
  }

  public void displayToast(String message) {
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
  }

  /**
   * 展示对话框
   *
   * @param view
   */
  public void onClickShowAlert(View view) {
    AlertDialog.Builder myAlertBuilder = new AlertDialog.Builder(this);
    myAlertBuilder.setTitle(R.string.alert_title);
    myAlertBuilder.setMessage(R.string.alert_message);
    myAlertBuilder.setPositiveButton(R.string.ok_button,
                                     new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        displayToast("Pressed OK");
      }
    });
    myAlertBuilder.setNegativeButton(R.string.cancel_button,
                                     new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        displayToast("Pressed Cancel");
      }
    });
    myAlertBuilder.show();
  }

  /**
   * 展示选择日期
   *
   * @param view
   */
  public void showDatePicker(View view) {
    DialogFragment fragment = new DatePickerFragment();
    fragment.show(getSupportFragmentManager(), getString(R.string.datepicker));
  }

  public void processDatePickerResult(int year, int month, int day) {
    String month_string = Integer.toString(month + 1);
    String day_string = Integer.toString(day);
    String year_string = Integer.toString(year);
    String dateMessage = (year_string + "-" + month_string + "-" + day_string);

    displayToast(dateMessage);
  }
}